package sk.naive.talker.util;

import java.text.*;
import java.util.logging.*;
import java.util.Date;
import java.io.*;

/**
 * LogFormatter.
 * <p/>
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.1 $ $Date: 2004/09/05 18:45:24 $
 */
public class LogFormatter extends Formatter {
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public String format(LogRecord record) {
		StringBuilder sb = new StringBuilder();
		String msg = record.getMessage();
		Object[] params = record.getParameters();
		if (params != null && params.length > 0) {
			msg = MessageFormat.format(msg, params);
		}
		sb.delete(0, sb.length());
		sb.append(sdf.format(new Date(record.getMillis())));
		sb.append(" " + record.getLoggerName() + " " + record.getSourceClassName() + "." + record.getSourceMethodName() + "()\n");
		sb.append("    " + record.getLevel() + ": " + msg + "\n");
		Throwable t = record.getThrown();
		if (t != null) {
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			t.printStackTrace(pw);
			pw.close();
			sb.append(sw.toString());
		}
		return sb.toString();
	}
}
